package com.mashiro.system.service.impl;

import com.mashiro.common.utils.DateUtils;
import com.mashiro.system.domain.MoeFund;
import com.mashiro.system.mapper.MoeFundMapper;
import com.mashiro.system.service.IMoeFundService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 资金Service业务层处理
 *
 * @author mashiro
 * @date 2023-04-15
 */
@Service
public class MoeFundServiceImpl implements IMoeFundService {
    @Resource
    private MoeFundMapper moeFundMapper;

    /**
     * 查询资金
     *
     * @param id 资金主键
     * @return 资金
     */
    @Override
    public MoeFund selectMoeFundById(Long id) {
        return moeFundMapper.selectMoeFundById(id);
    }

    /**
     * 查询资金列表
     *
     * @param moeFund 资金
     * @return 资金
     */
    @Override
    public List<MoeFund> selectMoeFundList(MoeFund moeFund) {
        return moeFundMapper.selectMoeFundList(moeFund);
    }

    /**
     * 新增资金
     *
     * @param moeFund 资金
     * @return 结果
     */
    @Override
    public int insertMoeFund(MoeFund moeFund) {
        moeFund.setCreateTime(DateUtils.getNowDate());
        moeFund.setTotalFunding(getTotalFunding(moeFund));
        moeFund.setUsedFunding(getUsedFunding(moeFund));
        return moeFundMapper.insertMoeFund(moeFund);
    }

    /**
     * 修改资金
     *
     * @param moeFund 资金
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMoeFund(MoeFund moeFund) {
        int i = 0;
        moeFund.setUpdateTime(DateUtils.getNowDate());
        moeFund.setTotalFunding(getTotalFunding(moeFund));
        if (moeFund.getTaskId() != 0L){// 项目的更新请求不携带使用数据，故只更新任务使用量
            moeFund.setUsedFunding(getUsedFunding(moeFund));
        }
        i += moeFundMapper.updateMoeFund(moeFund);
        if (moeFund.getTaskId() != 0L) {// 项目，任务的每次资金变动，均需要更新到项目
            MoeFund projectFund = moeFundMapper.selectMoeFundByProjectId(moeFund.getProjectId());
            MoeFund projectUsedFund = moeFundMapper.getProjectUsedFunding(projectFund.getProjectId());
            projectFund.setUsedProvinceFunding(projectUsedFund.getUsedProvinceFunding());
            projectFund.setUsedCityFunding(projectUsedFund.getUsedCityFunding());
            projectFund.setUsedCompanyFunding(projectUsedFund.getUsedCompanyFunding());
            projectFund.setUsedFunding(getUsedFunding(projectFund));
            i += moeFundMapper.updateMoeFund(projectFund);
        }
        return i;
    }

    /**
     * 批量删除资金
     *
     * @param ids 需要删除的资金主键
     * @return 结果
     */
    @Override
    public int deleteMoeFundByIds(Long[] ids) {
        return moeFundMapper.deleteMoeFundByIds(ids);
    }

    /**
     * 删除资金信息
     *
     * @param id 资金主键
     * @return 结果
     */
    @Override
    public int deleteMoeFundById(Long id) {
        return moeFundMapper.deleteMoeFundById(id);
    }

    /**
     * 是否存在资金子节点
     *
     * @param id 资金ID
     * @return 结果
     */
    @Override
    public boolean hasChildByFundId(Long id) {
        MoeFund moeFund = moeFundMapper.selectMoeFundById(id);
        if (!Objects.isNull(moeFund)) {
            if (moeFund.getTaskId() != 0) {
                return false;
            }
            int result = moeFundMapper.hasChildByFundId(moeFund.getProjectId());
            return result > 0;
        }
        return false;
    }

    /**
     * 是否存在资金子节点
     *
     * @param projectId 项目ID
     * @param taskId 任务ID
     * @return 结果
     */
    @Override
    public MoeFund getFundInfo(Long projectId, Long taskId) {
        return moeFundMapper.getFundInfo(projectId, taskId);
    }

    /**
     * 计算项目已使用资金情况
     *
     * @param projectId 项目ID
     * @return 结果
     */
    @Override
    public MoeFund getProjectUsedFunding(Long projectId) {
        return moeFundMapper.getProjectUsedFunding(projectId);
    }

    @Override
    public List<MoeFund> getMoeFundListByProjectIds(MoeFund moeFund, Long[] ids) {
        return moeFundMapper.selectMoeFundListByProjectIds(moeFund, ids);
    }
}
